﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using UMLSynthesizer.LangAnalyzer;

namespace UMLSynthesizer.Tasks.View.Converters
{
    class MetricNamesConverter : IValueConverter
    {
        #region Declarations

        readonly string ACDName = "ACD (Average component dependency)";
        readonly string CCCName = "CCC (Comulative cyclomatic complexity)";
        readonly string CCDName = "CCD (Comulative component dependency)";
        readonly string CDName = "CD (Component dependency)";
        readonly string MCCName = "MCC (Max cyclomatic complexity)";
        readonly string NCCDName = "NCCD (Normalized cumulative component dependency)";

        #endregion

        #region Public interface

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                LanguageAnalyzer.Metrics metric = (LanguageAnalyzer.Metrics)value;
                switch (metric)
                {
                    case LanguageAnalyzer.Metrics.ACD:
                        return ACDName;
                    case LanguageAnalyzer.Metrics.CCC:
                        return CCCName;
                    case LanguageAnalyzer.Metrics.CCD:
                        return CCDName;
                    case LanguageAnalyzer.Metrics.CD:
                        return CDName;
                    case LanguageAnalyzer.Metrics.MCC:
                        return MCCName;
                    case LanguageAnalyzer.Metrics.NCCD:
                        return NCCDName;
                    default:
                        return "";
                }
            }
            catch (Exception)
            {
                if (value as IEnumerable<LanguageAnalyzer.Metrics> != null)
                {
                    return from singleValue in value as IEnumerable<LanguageAnalyzer.Metrics>
                           select Convert(singleValue, targetType, parameter, culture);
                }
            }

            throw new ArgumentException("Invalid value object type");
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value as string != null)
            {
                string metricName = (value as string).Split(new char[] { ' ' })[0].ToLower();
                switch (metricName)
                {
                    case "acd":
                        return LanguageAnalyzer.Metrics.ACD;
                    case "ccc":
                        return LanguageAnalyzer.Metrics.CCC;
                    case "ccd":
                        return LanguageAnalyzer.Metrics.CCD;
                    case "cd":
                        return LanguageAnalyzer.Metrics.CD;
                    case "mcc":
                        return LanguageAnalyzer.Metrics.MCC;
                    case "nccd":
                        return LanguageAnalyzer.Metrics.NCCD;
                    default:
                        throw new ArgumentException("Invalid metric name");
                }
            }
            else if (value as IEnumerable<string> != null)
            {
                return from singleValue in value as IEnumerable<string>
                       select ConvertBack(singleValue, targetType, parameter, culture);
            }

            throw new ArgumentException("Invalid value object type");
        }

        #endregion
    }
}
